package com.khshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khshop.pojo.User;
import com.khshop.mapper.UserMapper;
import com.khshop.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 微信用户 服务实现类
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public IPage<User> findAllUser(Map<String,Object> map,Integer page, Integer limit) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Page<User> iPage = new Page<User>(page, limit);
        for (String key : map.keySet()) {
            queryWrapper = queryWrapper.like(key, map.get(key));
        }

        //queryWrapper.like("wname",map.get("wname")).like("tele",map.get("tele")).like("openid",map.get("openid"));

        return userMapper.selectPage(iPage, queryWrapper);
    }

    @Override
    public User findUserById(Long id) {
        return userMapper.selectById(id);
    }

    //通过openid登录
    @Override
    public User findUserByOpenId(Long openid){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean addUser(User user) {
        return userMapper.insert(user)>0;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateById(user)>0;
    }

    @Override
    public boolean delUser(Long id) {
        return userMapper.deleteById(id)>0;
    }
}
